package StudyCode.Concurrent.Bounce;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lixiaoyao
 */
public class BallComponent extends JPanel {

	private static final int DEFAULT_WIDTH = 450;
	private static final int DEFAULT_HEIGHT = 350;

	private List<Ball> balls = new ArrayList<>();

	public void add(Ball ball) {
		this.balls.add(ball);
	}

	@Override
	protected void paintComponent(Graphics graphics) {
		super.paintComponent(graphics); // erase background
		Graphics2D graphics2D = (Graphics2D) graphics;
		for (Ball ball : this.balls) {
			graphics2D.fill(ball.getShape());
		}
	}

	@Override
	public Dimension getPreferredSize() {
		return new Dimension(DEFAULT_WIDTH,DEFAULT_HEIGHT);
	}
}
